package com.shengfeng.mir2.controller;

import com.shengfeng.mir2.model.User;
import com.shengfeng.mir2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 把今天最好的表现当作明天最新的起点..
 * いま最高の表现として明日最新の始発..
 *
 * @author：小莫(83387856)
 * @github：https://github.com/qq83387856
 * @Email：hupengbest@163.com
 * @date: 14-5-20 13:44
 * @描述：demo
 **/
@Controller
public class IndexController {

    @Autowired
    public UserService userService;

    /**
     * list
     *
     * @return
     */
    @RequestMapping(value = "list")
    public String list(@ModelAttribute("form") User user) {
        List<User> list = userService.selectAll();
        user.setList(list);
        return "list";
    }

    /**
     * @return
     */
    @RequestMapping(value = "jumpToAdd")
    public String jumpToAdd() {
        return "add";
    }

    /**
     * add
     * @param name
     * @param password
     * @return
     */
    @RequestMapping(value = "add")
    public String add(@RequestParam("name") String name, @RequestParam("password") String password) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        userService.insertUser(user);
        return "redirect:list";
    }

    /**
     * edit
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "showUserDetail")
    public String showUserDetail(@ModelAttribute("form") User user) {
        User u = userService.selectUser(user);
        user.setId(u.getId());
        user.setName(u.getName());
        user.setPassword(u.getPassword());
        return "edit";
    }

    /**
     * edit
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "edit")
    public String update(@ModelAttribute("form") User user) {
        userService.updateUser(user);
        return "redirect:list";
    }

    /**
     * del
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "del")
    public String del(@ModelAttribute("form") User user) {
        userService.deleteUser(user.getId());
        return "redirect:list";
    }

}
